package com.yy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yy.bean.Employee;
import com.yy.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @RequestMapping("/toIndex")
    public String hello() {
        System.out.println("==================");
        return "index";
    }

    //获取所有雇员信息
    @RequestMapping("/getAllEmps")
    public String getAllEmps(@RequestParam(value = "pn", defaultValue = "1") Integer pn,
                             Model model) {
        System.out.println("pn = " + pn);
        //分页插件的代码
        PageHelper.startPage(pn, 3);
        List<Employee> emps = employeeService.getAllEmps();
        //返回值可以放到 PageInfo中
        PageInfo pageInfo = new PageInfo(emps, 2);
        //session.setAttribute("emps",emps);
        System.out.println("pageInfo = " + pageInfo);
        //session.setAttribute("page",pageInfo);
        model.addAttribute("page", pageInfo);
        return "allEmps";
    }

    @RequestMapping("/getAllEmps2")
    //把返回的数据变成json 格式的数据
    @ResponseBody
    public List<Employee> getAllEmps2() {
        return employeeService.getAllEmps();
    }

    @RequestMapping("/getEmpsByCondition")
    public String getEmpsByEmail(String emial) {
        employeeService.getEmpsByEmail(emial);
        return "success";
    }

    @RequestMapping("/editEmpById")
    @ResponseBody
    public Map<String, String> editEmpById(@RequestBody Employee employee) {
        Map<String, String> map = new HashMap<>();
        System.out.println("employee = " + employee);
        int i = 0;
        if (employee.getEmpId() == null) {
            System.out.println("没有id");
            i = employeeService.addEmp(employee);
            System.out.println("这里执行了吗");
        } else {
            System.out.println("有id");
            i = employeeService.updateEmpById(employee);
        }
        if (i > 0) {
            map.put("msg", "success");
        } else {
            map.put("msg", "fail");
        }
        return map;
    }

    @RequestMapping(value = "/getEmpByName", method = RequestMethod.GET)
    @GetMapping()
    @ResponseBody
    public List<Employee> getEmpByName(String empName) {
        if ("".equals(empName)) {
            return employeeService.getAllEmps();
        }
        System.out.println("empName = " + empName);

        return employeeService.getEmpsByName(empName);
    }

    // @RequestMapping("/deleteEmpById")
    @DeleteMapping("/deleteEmpById")
    @ResponseBody
    public Map<String, String> deleteEmpById(Integer empId) {
        System.out.println("empId = " + empId);
        int i = employeeService.deleteEmpById(empId);
        Map<String, String> map = new HashMap<>();
        if (i > 0) {
            map.put("msg", "success");
        } else {
            map.put("msg", "fail");
        }
        return map;
    }
}